<?php
namespace app\common\model;

use think\model\Merge;
use think\Request;

class CmsContentModel extends Merge
{
    protected $name = 'cms_content'; //定义栏目表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = true;
    //类型自动转换
    protected $type = [
        'sort' => 'integer',
    ];
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = ['uid']; //插入
    protected $update = []; //修改
    // 定义关联模型列表
    protected $relationModel = ['CmsContentData'];
    // 定义关联外键
    protected $fk = 'aid';
    // 为混淆字段定义映射
    protected $mapFields = [
        'aid'      => 'cms_content.aid',
        'data_aid' => 'CmsContentData.aid',
    ];

    /*读取器*/
    protected function getStatusTextAttr($value, $data)
    {
        $status = [
            0 => '<span class="label label-warning">待审</span>',
            1 => '<span class="label label-success">已审</span>',
        ];
        return $status[$data['status']];
    }

    protected function getContentAttr($value)
    {
        return stripslashes(htmlspecialchars_decode($value));
    }

    public function getExtendAttr($value, $data)
    {
        if (empty($data['extend'])) return '';
        $data['extend'] = explode(",", $data['extend']);
        $result         = [];
        foreach ($data['extend'] as $k => $v) {
            $v          = explode(":", $v);
            $result[$k] = ['key' => $v[0], 'value' => $v[1]];
        }
        return $result;
    }

    /*修改器*/
    protected function setExtendAttr($value, $data)
    {
        if ($data['extend']) {
            foreach ($data['extend']['key'] as $k => $v) {
                $key = str_replace(':','：',$v);
                $key = str_replace(',','，',$key);
                $value = str_replace(':','：',$data['extend']['value'][$k]);
                $value = str_replace(',','，',$value);
                $list[$k] = $key . ":" . $value;
            }
            $list = implode(",", $list);
            return $list;
        }
        return '';
    }

    protected function setDescriptionAttr($value, $data)
    {
        /*
        if (empty($value)) {
            $str = del_html(html_out($data['content']));
            if (empty($str)) {
                return $data['title'];
            }
            return substr($str, 0, 240);
        }
        */
        return $value;
    }

    protected function setFlagAttr($value, $data)
    {
        if (empty($value)) {
            return '';
        } else {
            return implode(',', $value);
        }
    }

}